package com.zito.health.api.utils;

import com.zito.health.common.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


/**
 * Jedis配置，项目启动注入JedisPool
 */
@Configuration
@EnableAutoConfiguration
public class JedisConfig {

    /**
     * logger
     */
    private static final Logger logger = LoggerFactory.getLogger(JedisConfig.class);

    private String host;

    private int port;

    private String password;

    private int timeout;

//    @Value("${redis.pool.max-active}")
//    private int maxActive;
//
//    @Value("${redis.pool.max-wait}")
//    private int maxWait;
//
//    @Value("${redis.pool.max-idle}")
//    private int maxIdle;
//
//    @Value("${redis.pool.min-idle}")
//    private int minIdle;

    @Bean
    public JedisPool redisPoolFactory() {
        try {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxIdle(100);
            jedisPoolConfig.setMaxWaitMillis(-1);
            jedisPoolConfig.setMaxTotal(10000);
            jedisPoolConfig.setMinIdle(0);
            // JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
            String pwd = StringUtil.isBlank(password) ? null : password;
            JedisPool jedisPool = new JedisPool(jedisPoolConfig, "121.89.219.252", 6379, 100000, "toppm");
            logger.info("初始化Redis连接池JedisPool成功!地址: " + "121.89.219.252" + ":" + 6379);
            return jedisPool;
        } catch (Exception e) {
            logger.error("初始化Redis连接池JedisPool异常:" + e.getMessage());
        }
        return null;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

}

